<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Func;
use App\Models\AdminUser;
use App\Models\Guahao;
use App\Models\GuahaoSet;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;


class GuahaoController extends Controller
{
    use Func;

    // 列表
    public function list(Request $request)
    {
		// dd($request->all());
		$adminUser = AdminUser::where('id', $request->user_id)->first();
        $keywords = trim($request->keywords);
        $data = Guahao::with('jiuzhenren', 'guahaoSet')
		->where('status', 0)
		->whereIn('keshi', explode(',', $adminUser->keshi))
		->where('date_time', '>=', date('Y-m-d'))
		->orderBy('date_time', 'asc')
		->orderBy('yuyuetime_id', 'asc')
		->paginate($request->limit ?? 10);
        return $this->returnJson(0, 'success', $data);
    }

    // 删除
    public function del(Request $request)
    {
        if (!Guahao::where('id', $request->id)->delete()) {
            return $this->returnJson(1, '删除失败');
        }

        return $this->returnJson(0, '删除成功');
    }
	
	// 挂号设置操作
	public function getSetList(Request $request)
	{
	    $data = GuahaoSet::orderBy('id', 'asc')->get();
	    return $this->returnJson(0, 'success', $data);
	}
	
	// 添加
	public function addSet(Request $request)
	{
	    if (!$res = GuahaoSet::create([
	        'start_time' => $request->start_time,
			'end_time' => $request->end_time,
			'nums' => $request->nums,
	        'created_at' => date('Y-m-d H:i:s')
	    ])) {
	        return $this->returnJson(1, '添加失败', $res);
	    }
	
	    return $this->returnJson(0, '添加成功', $res);
	}
	
	
	// 删除
	public function delSet(Request $request)
	{
	    if (!GuahaoSet::where('id', $request->id)->delete()) {
	        return $this->returnJson(1, '删除失败');
	    }
	
	    return $this->returnJson(0, '删除成功');
	}
	
	// 获得
	public function getSet(Request $request)
	{
		// dump($request->all());exit;
	    $res = GuahaoSet::where('id', $request->id)->first();
	
	    return $this->returnJson(0, 'success', $res);
	}
	
	// 编辑
	public function editSet(Request $request)
	{
	    // dd($request->addAccount);
	    if (!$res = GuahaoSet::where('id', $request->id)->update([
	        'start_time' => $request->start_time,
			'end_time' => $request->end_time,
			'nums' => $request->nums,			
	    ])) {
	        return $this->returnJson(1, '修改失败', $res);
	    }
	
	    return $this->returnJson(0, '修改成功', $res);       
	}
}
